var prefix = jsctx + "/acctTenantOperator";

var acctBillOrder = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 80;

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageAcctBillOrder",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    pageSize: 20,
                    pageList: [10, 20, 30],
                    pageNumber: 1,
                    sidePagination: "server",
                    cache: false,
                    paginationShowPageGo: true,
                    height: h,
                    rightFixedColumns: true,
                    showColumns: false,
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            tenantOperatorCode: $('#tenantOperatorCode').val(),
                            billOrderNum: $('#billOrderNum').val(),
                            elecStationId: $('#elecStationId').val(),
                            status: $('#status').val(),
                            startTime: $('#startTime').val(),
                            endTime: $('#endTime').val(),
                            //运营商为0,分销商为1
                            userType: 1,
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '', width: '50', align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'billOrderNum', title: '结算订单号', width: '120', align: 'left'},
                        {field: 'stationName', title: '站点', width: '120', align: 'left'},
                        {field: 'orderCount', title: '订单数', width: '80', align: 'right'},
                        {field: 'totalPower', title: '电量', width: '80', align: 'right'},
                        {field: 'totalElecMoney', title: '电费', width: '80', align: 'right'},
                        {field: 'totalSeviceMoney', title: '服务费', width: '80', align: 'right'},
                        {field: 'totalDiscountMoney', title: '优惠金额', width: '80', align: 'right'},
                        {field: 'totalMoney', title: '总金额', width: '80', align: 'right'},
                        {field: 'payAmount', title: '实付金额', width: '80', align: 'right'},
                        {
                            field: '', title: '账单日', width: '120', align: 'left',
                            formatter: function (value, row, index) {
                                if (row.acctDayString!=''&&row.acctDayString!=null){
                                    return row.acctDayString + '号'
                                }
                                return '-';
                            }
                        },
                        {
                            field: '', title: '账单时间', width: '180', align: 'left',
                            formatter: function (value, row, index) {
                                return row.billStartTime + '至' + row.billEndTime
                            }
                        },
                        {field: 'runTime', title: '出账时间', width: '120', align: 'left'},

                        {
                            field: '', title: '结算规则', width: '150', align: 'left',
                            formatter: function (value, row, index) {
                                if ((row.elecAcctRule!=''&&row.elecAcctRule!=null)&&(row.serviceAcctRule!=''&&row.serviceAcctRule!=null)){
                                    return '结算电费=' + row.elecAcctRule + ';结算服务费=' + row.serviceAcctRule
                                }
                                return '-'
                            }
                        },
                        {field: 'acctRuleRemark', title: '规则说明', width: '150', align: 'left',},
                        {field: 'billTotalElecMoney', title: '规则电费', width: '120', align: 'right',},
                        {field: 'billTotalSeviceMoney', title: '规则服务费', width: '120', align: 'right',},
                        {field: 'billTotalMoney', title: '规则总金额', width: '120', align: 'right',},
                        // {field: 'diffElecMoney', title: '异常订单电费差值', width: '120', align: 'right',},
                        // {field: 'diffServiceMoney', title: '异常订单服务费差值', width: '120', align: 'right',},
                        {field: 'adjustElecAmount', title: '调整电费金额', width: '120', align: 'right',},
                        {field: 'adjustServiceAmount', title: '调整服务费金额', width: '120', align: 'right',},
                        // {field: 'adjustPayAmount', title: '调整金额', width: '120', align: 'right',},
                        {field: 'remark', title: '调整备注', width: '100', align: 'right',},
                        {field: 'supplyRemark', title: '补充账单备注', width: '130', align: 'left',},
                        // {field: 'adjustPayAmount', title: '调整金额', width: '120', align: 'right',},
                        // {field: 'remark', title: '调整备注', width: '120', align: 'right',},
                        {
                            field: 'elecAcctAmount', title: '结算电费', width: '120', align: 'right',
                        },
                        {
                            field: 'serviceAcctAmount', title: '结算服务费', width: '120', align: 'right',
                        },
                        {
                            field: 'totalAcctAmount', title: '结算总金额', width: '120', align: 'right',
                        },
                        {
                            field: '', title: '状态', width: '120', align: 'left', class:
                                'operateBefore-td',
                            formatter: function (value, row, index) {
                                if (row.status == '0') {
                                    return '待审核';
                                } else if (row.status == '1') {
                                    return '待确认账单';
                                } else if (row.status == '2') {
                                    return '复核中';
                                } else if (row.status == '3') {
                                    return '结算完成';
                                }
                            }
                        },
                        {
                            title: '操作',
                            field: 'roleId',
                            width: '250', align: 'center',
                            class: 'operate-td',
                            formatter: function (value, row, index) {
                                var a = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="详情" onclick="acctBillOrder.detailAcctBillOrderOperator(\''
                                    + row.billOrderId
                                    + '\')">详情</a> ';

                                var b = '<a class="btn btn-success btn-sm" href="#" title="订单" onclick="acctBillOrder.orderAcctBillOrder(\''
                                    + row.billOrderId
                                    + '\')">订单</a> ';

                                // var c = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="审核" onclick="acctBillOrder.saveReviewAcctBillOrder(\''
                                //     + row.billOrderId
                                //     + '\')">审核</a> ';

                                var d = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="处理" onclick="acctBillOrder.saveDealWithAcctBillOrder(\''
                                    + row.billOrderId + '\',\'' + row.adjustElecAmount + '\',\'' + row.adjustServiceAmount + '\',\'' + row.billTotalMoney
                                    + '\')">处理</a> ';

                                // var e = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="复核" onclick="acctBillOrder.saveReviewAcctBillOrder(\''
                                //     + row.billOrderId
                                //     + '\')">复核</a> ';

                                var g = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="详情" onclick="acctBillOrder.detailSupplyAcctBillOrderOperator(\''
                                    + row.billOrderId
                                    + '\')">详情</a> ';

                                if (row.status == '1') {
                                    if (row.acctRuleId==null||row.acctRuleId==''){
                                        return  d + g ;
                                    }
                                    return a + b + d;
                                } else {
                                    if (row.acctRuleId==null||row.acctRuleId==''){
                                        return g ;
                                    }
                                    return a + b;
                                }

                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function goBack() {
        location.href = prefix + "/index";
    }

    function detailAcctBillOrder(billOrderId) {
        location.href = prefix + "/detailAcctBillOrder/" + billOrderId;
    }

    function acctTenantOperatorElecStation(tenantOperatorCode) {
        location.href = prefix + "/acctTenantOperatorElecStation/" + tenantOperatorCode;
    }

    function acctBank(tenantOperatorCode) {
        location.href = prefix + "/acctBank/" + tenantOperatorCode;
    }

    function orderAcctBillOrder(billOrderId) {
        location.href = prefix + "/orderAcctBillOrder/" + billOrderId;
    }

    function saveDealWithAcctBillOrder(billOrderId,adjustElecAmount,adjustServiceAmount,billTotalMoney) {
        $(".confirmModal").removeClass("hide");
        $("#billOrderId").val(billOrderId);
        $("#adjustElecAmount").val(adjustElecAmount);
        $("#adjustServiceAmount").val(adjustServiceAmount);
        $("#billTotalMoney").val(billTotalMoney);

    }

    var isajaxsend = true;

    $(".buttonBlock .cancleButton").click(function () {
        if (isajaxsend) {
            var remark=$("#rejectRemark").val();
            if (remark==''){
                alert("请填写驳回原因");
                return;
            }
            $.ajax({
                url: prefix + "/saveReviewAcctBillOrder",
                type: "post",
                data: {
                    'billOrderId': $("#billOrderId").val(),
                    "rejectRemark":remark,
                    'status': '2'
                }, beforeSend: function () {
                    isajaxsend = false
                },
                success: function (data) {
                    // layer.close(index);
                    if (data.status == 200) {
                        isajaxsend = true
                        $(".confirmModal").addClass("hide");
                        layer.msg("处理成功");
                        $("#rejectRemark").val('');
                        window.history.back()
                        reLoad();
                    } else {
                        isajaxsend = true
                        layer.msg(data.message);
                    }
                }
            });
        }
    })
    $(".buttonBlock .confirmButton").click(function () {
        if (isajaxsend) {
            $.ajax({
                url: prefix + "/saveReviewAcctBillOrder",
                type: "post",
                data: {
                    'billOrderId': $("#billOrderId").val(),
                    'adjustElecAmount': $("#adjustElecAmount").val(),
                    'adjustServiceAmount': $("#adjustServiceAmount").val(),
                    'billTotalMoney': $("#billTotalMoney").val(),
                    'status': '3'
                },
                beforeSend: function () {
                    isajaxsend = false
                },
                success: function (data) {
                    // layer.close(index);
                    if (data.status == 200) {
                        isajaxsend = true
                        $(".confirmModal").addClass("hide");
                        layer.msg("您的账单收益金额已进入钱包，可以随时发起提现");
                        $("#rejectRemark").val('');
                        window.history.back()
                        reLoad();
                    } else {
                        isajaxsend = true
                        layer.msg(data.message);
                    }
                }
            });
        }
    })

    function exportAcctBillOrder() {
        $.fileDownload(prefix + "/exportAcctBillOrder", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                tenantOperatorCode: $("#tenantOperatorCode").val().trim(),
                billOrderNum: $("#billOrderNum").val().trim(),
                elecStationId: $("#elecStationId").val().trim(),
                status: $("#status").val().trim(),
                startTime: $("#startTime").val().trim(),
                endTime: $("#endTime").val().trim(),
                userType: 1,
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...", {time: -1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！", {time: 1500});
            },
            failCallback: function (data) {
                var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                layer.msg(JSON.parse(str).message);
            }
        })
    }
    function detailAcctBillOrderOperator(billOrderId) {
        location.href = prefix + "/detailAcctBillOrderOperator/" + billOrderId;
    }

    function detailSupplyAcctBillOrderOperator(billOrderId) {
        location.href = prefix + "/detailSupplyAcctBillOrderOperator/" + billOrderId;
    }

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        acctBank: acctBank,
        detailAcctBillOrderOperator: detailAcctBillOrderOperator,
        orderAcctBillOrder: orderAcctBillOrder,
        // saveReviewAcctBillOrder: saveReviewAcctBillOrder,
        exportAcctBillOrder: exportAcctBillOrder,
        saveDealWithAcctBillOrder: saveDealWithAcctBillOrder,
        detailSupplyAcctBillOrderOperator:detailSupplyAcctBillOrderOperator
    };
})();
